package com.mvvm.start.main.mvvm.v.fragment.main

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
//import com.theartofdev.edmodo.cropper.CropImage
//import com.theartofdev.edmodo.cropper.CropImageView
import coil.load
import com.mvvm.start.base.base.BaseFragment
import com.mvvm.start.base.utils.*
import com.mvvm.start.common.utils.RouteUtils
import com.mvvm.start.main.R
import com.mvvm.start.main.databinding.FragmentMineBinding
import com.mvvm.start.main.mvvm.v.activity.SwitchThemeActivity
import com.mvvm.start.main.mvvm.v.activity.TestActivity
import com.mvvm.start.main.mvvm.v.activity.test9.NinePathPictureActivity
import com.mvvm.start.main.mvvm.vm.MineFragmentViewModle


/**
 * @author lmy
 * @功能: 我的Fragment
 * @Creat 12/23/20 12:09 PM
 * @Compony 465008238@qq.com
 */
class MineFragment : BaseFragment<MineFragmentViewModle, FragmentMineBinding>() {
    override fun onResume() {
        super.onResume()
        initMineUserInfo()
    }

    /**
     * 初始化Mine界面的数据
     */
    private fun initMineUserInfo() {
        viewBinding.ivHead.load("http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png")
        if (!CommonUtils.isLogin()) {
            viewBinding.tvNickname.text = "点击登录"
            viewBinding.topIvVip.setImageResource(R.drawable.ic_mine_new_state_mianfei)
        } else {
            viewBinding.tvNickname.text = CommonUtils.getUserName()
            viewBinding.topIvVip.setImageResource(if (CommonUtils.isVip()) R.drawable.ic_mine_new_state_vip else R.drawable.ic_mine_new_state_mianfei)
        }
    }


    @SuppressLint("InvalidWakeLockTag")
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initViews() {
        viewBinding.apply {
            smartlayout.apply {
                setEnableLoadMore(false)
                setOnRefreshListener {
                    mViewModel.getUserInfo()
                }
            }
            ktsetOnClickListener(ivHead, tvNickname, llCapitaldetails, ivSetting) {
                when (this) {
                    ivSetting -> {
                        ktstartActivity<SwitchThemeActivity>()
                    }
                    llCapitaldetails -> {
                    }
                    ivHead -> {
                        ktstartActivity<NinePathPictureActivity>()
//                    PermissionXUtils.requestPermissions(
//                        this@MineFragment,
//                        mutableListOf(
//                            Manifest.permission.CAMERA,
//                            Manifest.permission.READ_EXTERNAL_STORAGE,
//                            Manifest.permission.WRITE_EXTERNAL_STORAGE
//                        ), {
//                            //调用相册选择图片
//                            var photoPickerIntent = Intent(Intent.ACTION_PICK);
//                            photoPickerIntent.type = "image/*";
//                            startActivityForResult(
//                                photoPickerIntent,
//                                ConstantUtils.REQUEST_TAKEPHOTO_CODE
//                            )
//                        }, {
//                            "拒绝存储权限会导致软件更新无法使用".toast()
//                        }, "修改头用户头像需要开启权限才可使用"
//                    )
                    }
                    tvNickname -> {
                        RouteUtils.startLoginLoginActivity()
                    }
                }
            }

        }
    }


    override fun initObserve() {
        mViewModel.apply {
            userInfo.observe(this@MineFragment, Observer {
                viewBinding.smartlayout.finishRefresh()
                initMineUserInfo()
                if (it != null) {
                    if (it?.phone.isNullOrEmpty()) {
                    }
                    return@Observer
                }
            })
        }
    }

    override fun initData() {
    }


}